package com.jyniubi.gmail.sms.service.impl;

import com.jyniubi.gmail.sms.entity.SkuFullReductionEntity;
import com.jyniubi.gmail.sms.entity.SkuLadderEntity;
import com.jyniubi.gmail.sms.mapper.SkuFullReductionMapper;
import com.jyniubi.gmail.sms.mapper.SkuLadderMapper;
import com.jyniubi.gmail.sms.service.SkuFullReductionService;
import com.jyniubi.gmail.sms.vo.ItemSaleVo;
import com.jyniubi.gmail.sms.vo.SkuSalesVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyniubi.gmail.common.bean.PageResultVo;
import com.jyniubi.gmail.common.bean.PageParamVo;

import com.jyniubi.gmail.sms.mapper.SkuBoundsMapper;
import com.jyniubi.gmail.sms.entity.SkuBoundsEntity;
import com.jyniubi.gmail.sms.service.SkuBoundsService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import sun.dc.pr.PRError;

import javax.annotation.Resource;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBoundsEntity> implements SkuBoundsService {

    @Resource
    private SkuFullReductionMapper skuFullReductionMapper;
    @Resource
    private SkuLadderMapper skuLadderMapper;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuBoundsEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public void saveSales(SkuSalesVo skuSalesVo) {
        //3.1保存sms_sku_bounds
        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        BeanUtils.copyProperties(skuSalesVo,skuBoundsEntity);
        List<Integer> works = skuSalesVo.getWork();
        if (!CollectionUtils.isEmpty(works)){
            skuBoundsEntity.setWork(works.get(3) * 8 + works.get(2) * 4 + works.get(1) * 2 + works.get(0));
        }
        this.save(skuBoundsEntity);
        //3.2保存sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuSalesVo,skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuSalesVo.getFullAddOther());
        this.skuFullReductionMapper.insert(skuFullReductionEntity);
        //3.3保存sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuSalesVo,skuLadderEntity);
        skuLadderEntity.setAddOther(skuSalesVo.getLadderAddOther());
        this.skuLadderMapper.insert(skuLadderEntity);
    }

    @Override
    public List<ItemSaleVo> querySalesBySkuId(Long skuId) {
        List<ItemSaleVo> itemSaleVos = new ArrayList<>();
        SkuBoundsEntity skuBoundsEntity = this.getOne(new QueryWrapper<SkuBoundsEntity>().eq("sku_id",skuId));
        if (skuBoundsEntity != null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setSaleId(skuBoundsEntity.getId());
            itemSaleVo.setType("积分");
            itemSaleVo.setDesc(new StringBuilder("送")
                    .append(new DecimalFormat("#").format(skuBoundsEntity.getGrowBounds()))
                    .append("成长积分，送")
                    .append(new DecimalFormat("#").format(skuBoundsEntity.getBuyBounds()))
                    .append("购物积分").toString());
            itemSaleVos.add(itemSaleVo);

        }
        SkuFullReductionEntity skuFullReductionEntity =
                this.skuFullReductionMapper.selectOne(new QueryWrapper<SkuFullReductionEntity>().eq("sku_id",skuId));
        if (skuFullReductionEntity != null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setSaleId(skuFullReductionEntity.getId());
            itemSaleVo.setType("满减");
            itemSaleVo.setDesc(new StringBuilder("满")
                    .append(new DecimalFormat("#.00").format(skuFullReductionEntity.getFullPrice()))
                    .append("减")
                    .append(new DecimalFormat("#.00").format(skuFullReductionEntity.getReducePrice()))
                    .toString());
            itemSaleVos.add(itemSaleVo);
        }

        SkuLadderEntity skuLadderEntity =
                this.skuLadderMapper.selectOne(new QueryWrapper<SkuLadderEntity>().eq("sku_id",skuId));
        if (skuLadderEntity != null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setSaleId(skuLadderEntity.getId());
            itemSaleVo.setType("打折");
            itemSaleVo.setDesc(new StringBuilder("满")
                            .append(new DecimalFormat("#").format(skuLadderEntity.getFullCount()))
                            .append("件打")
                            .append(new DecimalFormat("#.0").format(skuLadderEntity.getDiscount().divide(new BigDecimal(10))))
                            .append("折")
                            .toString());
            itemSaleVos.add(itemSaleVo);
        }
        return itemSaleVos;
    }

}